<?php

namespace app\controller;

use app\BaseController;
use app\service\ArticleService;
use app\service\CategoryService;
use app\service\GoodsService;
use think\App;
use think\exception\HttpException;
use think\facade\View;

class Article extends BaseController
{
    private $articleService;
    private $goodsService;
    /**
     * @var CategoryService|mixed|object
     */
    private $categoryService;

    /**
     * Category constructor.
     * @param App $app
     */
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->categoryService = app()->make(CategoryService::class);
        $this->articleService = app()->make(ArticleService::class);
        $this->goodsService = app()->make(GoodsService::class);
    }

    /**
     * @param $id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index($id): string
    {
        $article = $this->articleService->queryDetail($id);
        if (empty($article)) {
            return View::fetch('/error/index', array('msg'=>'抱歉，文章不存在'));
        }
        $this->articleService->updateCount('views', $id);
        $category = $this->categoryService->queryDetail($article['cid']);
        $parentCategory = $this->categoryService->queryDetail($category['parent_id']);
        $data = array('article' => $article, 'category' => $category, 'parentCategory' => $parentCategory);
        $data['settingDescription'] = $article['description'];
        $data['brandList'] = $this->goodsService->queryListByNum('brand_id > 0', 'commission_rate desc',5);
        return View::fetch('index', $data);
    }
}
